package com.example.ending_demo.servlet;

import com.example.ending_demo.model.Address;
import com.example.ending_demo.model.PageInfo;
import com.example.ending_demo.model.User;
import com.example.ending_demo.service.AddressService;

import java.io.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/SearchDoneAddressServlet")
public class SearchDoneAddressServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.访问数据库读取通讯录列表数据
		AddressService addSvc = new AddressService();
		//userid也是从Session中获取
		HttpSession session = request.getSession(false);
		User user = (User) session.getAttribute("user");
		Integer user_id = user.getUser_id();
		PageInfo<Address> page = null;
		// 获取请求参数 pageNo
		String pageNoParam = request.getParameter("pageNo");

		// 设置默认值为 1
		int pageNo = (pageNoParam != null && !pageNoParam.isEmpty()) ? Integer.parseInt(pageNoParam) : 1;

//        addrList = (ArrayList<Address>) addSvc.getAddressByUserID(1);
		page = addSvc.selectDonePage(pageNo,5);

		//2.通过请求转发将通讯数据转发到address_list.jsp页面
		request.setAttribute("searchPage",page);
		page.setUrl("SearchDoneAddressServlet?action=searchPage");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/search_page.jsp");
		dispatcher.forward(request,response);
    }
}